﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GalaxyDefenderChaos
{
    public class MainMenu : Menu
    {
        PlanarButton[] _ButtonList;

        public PlanarButton[] ButtonList
        {
            get { return _ButtonList; }
            set { _ButtonList = value; }
        }
        PlanarModel _Background;
        PlanarModel _Logo;
        private string[] _ButtonTextureStrings = { 
                                            "Menu/NewGame",
                                            "Menu/Option",
                                            "Menu/About",
                                            "Menu/Exit"
                                          };
        private string _BackgroundTextureString = "Menu/BackgroundMenu";
        private string _LogoTextureString = "Menu/Logo";
        Camera _Camera;

        public MainMenu(ContentManager content, GraphicsDevice graphicsDevice) 
        {
            _ButtonList = new PlanarButton[_ButtonTextureStrings.Count()];
            Vector2 backgroundSize = new Vector2(1024, 1024);
            Vector2 buttonSize = new Vector2(350, 64);
            Vector3 startPosition = new Vector3(0, (buttonSize.Y)*_ButtonList.Count()/2 - 100, 0);
            Vector3 offset = new Vector3(0, buttonSize.Y/1.5f, 0);
            for (int i = 0; i < 1; i++)//_ButtonTextureStrings.Count()
            {
                _ButtonList[i] = new PlanarButton(content, _ButtonTextureStrings[i], buttonSize, startPosition - offset*i, Vector3.Forward, Vector3.Up, graphicsDevice);
            }

            _Background = new PlanarModel(content, _BackgroundTextureString, backgroundSize, new Vector3(0, 0, -1), Vector3.Forward,Vector3.Up, graphicsDevice);
            _Logo = new PlanarModel(content, _LogoTextureString, new Vector2(700, 300), startPosition + offset * 1.5f, Vector3.Forward, Vector3.Up, graphicsDevice);
            /*
            _Camera = new PerspectiveCamera(new Vector3(0, 0, 700), new Vector3(0, 0, 0), Vector3.Up, 1f, 1000000f, MathHelper.PiOver4, graphicsDevice.Viewport.AspectRatio);
            */
            _Camera = new ChaseCamera(new Vector3(0, 400, 1500),
                    new Vector3(0, 200, 0),
                    new Vector3(0, 0, 0), graphicsDevice);
        }

        public override void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {
            graphicsDevice.RasterizerState = RasterizerState.CullNone;
            graphicsDevice.BlendState = BlendState.AlphaBlend;

            //_Background.Draw(graphicsDevice, _Camera);
            _Logo.Draw(graphicsDevice, _Camera, gameTime);
            for (int i = 0; i < 1; i++)//_ButtonList.Count()
            {
                _ButtonList[i].Draw(graphicsDevice, _Camera, gameTime);
            }

            graphicsDevice.BlendState = BlendState.Opaque;
        }

        public int GetMenuItemIndexTouched(Vector2 point)
        {
            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                if (_ButtonList[i].CheckInside(point, _Camera))
                    return i;
            }
            return -1;
        }

        //public int GetMenuItemIndexTouched(Vector2 point)
        //{
        //    for (int i = 0; i < _ButtonList.Count(); i++)
        //    {
        //        if (_ButtonList[i].CheckInside(point))
        //            return i;
        //    }
        //    return -1;
        //}
    }
}
